﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IntelligentCMS
{
    public partial class CMSRadioButtonMultilineBox : System.Web.UI.UserControl, iCMSControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region iCMSControl Members

        private string name = string.Empty;

        private bool checkforerrors = false;

        public string Name
        {
            set
            {
                name = value;
            }
        }

       

        public System.Web.UI.WebControls.Unit InternalWidth
        {
            set
            {
                //theTextBox.Width = value;
            }
        }

        public System.Web.UI.WebControls.Unit InternalHeight
        {
            set
            {
                //theTextBox.Height = value;
            }
        }


        private List<string> errorlist()
        {
            List<string> errorlist = new List<string>();


            foreach (CMSRadioButtonMultilineItem c in controls)
            {
                errorlist.AddRange(c.Errors);

            }
             
             
            return errorlist;
        }

        public void DataBind2()
        {

            string tablebegin = "<table width=\"100%\"><tr class=\"columnheader\" height=\"40px\">"; 
            string firstrow = string.Empty;
            foreach (string s in columns)
            {
                firstrow += "<td align=\"center\">" + s + "</td>";

            }

            string tableend = "</tr></table>";

            Literal_TableBegin.Text = tablebegin + firstrow;


            Literal_TableEnd.Text = tableend;
            bool alternate = false;
            foreach (string s in rows)
            {
                CMSRadioButtonMultilineItem newItem = (CMSRadioButtonMultilineItem)Page.LoadControl("~/CMSRadioButtonMultilineItem.ascx");
                newItem.RowHeader = s;
                newItem.Columns = columns;
                newItem.Alternate = alternate;
                newItem.ParentName = name;
                newItem.CheckForErrors = true;
                newItem.ClientURL = clienturl;
                
                newItem.DataBind2();

                thePanel.Controls.Add(newItem);
                controls.Add(newItem);

                alternate = !alternate;

            }
            

        }

        private List<CMSRadioButtonMultilineItem> controls = new List<CMSRadioButtonMultilineItem>();

        private List<string> columns = new List<string>();

        public List<string> Columns
        {
            set
            {
                columns = value;
            }

        }


        private string clienturl = string.Empty;

        public string ClientURL
        {
            set
            {
                clienturl = value;
            }


        }

        private List<string> rows = new List<string>();

        public List<string> Rows
        {
            set
            {
                rows = value;
            }

        }


        private bool haserrors()
        {
            if (errorlist().Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }


        private string generatesqlstring()
        {
            string sqlstring = string.Empty;

            foreach (CMSRadioButtonMultilineItem item in controls)
            {

                sqlstring += item.SQLString;
            }

            return sqlstring;

        }

        public string SQLString
        {
            get
            {
                return generatesqlstring();


            }
 
        }

        public string LabelText
        {
            set { theLabel.Text = value; }
        }

        public bool CheckForErrors
        {
            set { checkforerrors = value; }
        }

        public bool HasErrors
        {
            get { return haserrors(); }
        }

        #endregion

        #region iCMSControl Members

        public List<string> Errors
        {
            get { return errorlist(); }
        }

        #endregion
    }
}